package com.dcbeida.activity.stockdetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.RkSocketAdapter;
import com.dcbeida.adapter.SalsesDetailRYAdapter;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Stock;
import com.dcbeida.widget.ListViewForScrollView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 入存详情
 * Created by swt on 2017/9/29.
 */

public class RkStockDetailActivity extends BaseActivity {


    @Bind(R.id.tv_bh)
    TextView tvBh;
    @Bind(R.id.tv_right)
    TextView tvRight;

    @Bind(R.id.tv_rksj)
    TextView tvRksj;
    @Bind(R.id.tv_djsj)
    TextView tvDjsj;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_box_num)
    TextView tvBoxNum;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.ll_notfp)
    LinearLayout llNotfp;
    @Bind(R.id.myList)
    ListViewForScrollView myList;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    private Stock.ListBean resoursebean;
    private RkSocketAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.rk_detail;
    }

    @Override
    public void setData() {
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.rkxq);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("修改");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            resoursebean = (Stock.ListBean) bundle.getSerializable("Stock.ListBean");
            tvBh.setText(resoursebean.getStockinlogid());
            tvRksj.setText(mContext.getString(R.string.rksj2) + resoursebean.getAddstocktime());
            tvDjsj.setText(mContext.getString(R.string.djsj2) + resoursebean.getDeliverytime());
            tvName.setText(resoursebean.getProvider());
            tvMoney.setText(mContext.getString(R.string.yuan) + resoursebean.getInvoicemoney());
            tvBoxNum.setText(resoursebean.getQuantity() + "");
        }

        //图片地址
        ArrayList<String> paths = new ArrayList<>();
        if (!TextUtils.isEmpty(resoursebean.getDeliverimgurl())) {
            llNotfp.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
            if (resoursebean.getDeliverimgurl().indexOf(",") != -1) {
                String[] imgUrl = resoursebean.getDeliverimgurl().split(",");
                for (int i = 0; i < imgUrl.length; i++) {
                    paths.add(imgUrl[i]);
                }
            } else {
                paths.add(resoursebean.getDeliverimgurl());
            }
        } else {
            llNotfp.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerview.setHasFixedSize(true);
        SalsesDetailRYAdapter ryAdapter = new SalsesDetailRYAdapter(mContext);
        recyclerview.setAdapter(ryAdapter);
        ryAdapter.setData(paths);

        scrollView.smoothScrollTo(0, 0);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new RkSocketAdapter(this);
        myList.setAdapter(adapter);

        ArrayList<Stock.ListBean.DrugsBean> tempList = new ArrayList<>();
        DrugListBean listBean = MyApplication.getInstance().getmDrugBean();
        if (listBean == null || listBean.getList() == null)
            return;


        for (Drug drug : listBean.getList()) {
            List<Stock.ListBean.DrugsBean> drugList = new ArrayList<>();
            int count = 0;
            for (Stock.ListBean.DrugsBean bean : resoursebean.getDrugs()) {

                if (drug.getDrugid() == Integer.parseInt(bean.getDrugid())) {
                    count += Integer.parseInt(bean.getQuantity());
                    bean.setDrugName(drug.getDrugname());
                    drugList.add(bean);
                }


            }
            if (drugList.size() > 0) {
                Stock.ListBean.DrugsBean data = drugList.get(0);
                data.setFirst(true);
                data.setCount(count);
            }
            tempList.addAll(drugList);
            //tpdo
        }


        adapter.setmArrayList(tempList);


    }


    @OnClick({R.id.img_back,R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                showActivityForResult(RkStockDetailEditActivity.class, getIntent().getExtras(),0x111);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(RkStockDetailActivity.class.getName(), "#########requestCode:" + requestCode + ";######resultCode:" + resultCode);
        if (resultCode == RESULT_OK && requestCode == 0x111) {
            resoursebean = (Stock.ListBean) data.getSerializableExtra("bean");
            tvName.setText(resoursebean.getProvider());
            tvMoney.setText(mContext.getString(R.string.yuan) + resoursebean.getInvoicemoney());
        }
    }
}
